package com.example.shushiyunsst.fragment.main.home.child.fu_li_pool

import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.text.isDigitsOnly
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.normalShortToast
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.constant.CURRENT_DEVICE_TOKEN
import com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.sub.*
import com.example.shushiyunsst.util.ICON_FU_LI
import com.example.shushiyunsst.widget.water_view.FloatBallView
import com.example.shushiyunsst.widget.water_view.model.FloatBall
import com.example.wallet.db.UserInfoManager
import com.example.wallet.global.AppGlobals
import com.example.wallet.utils.Preference
import com.senon.modularapp.util.JssSpannableString

/**
 * 福利池
 * */
@Route(path = ICON_FU_LI)
class FuLiPoolFragment : BaseFragmentEx(), View.OnClickListener {

    companion object {
        fun newInstance(): FuLiPoolFragment {
            val args = Bundle()

            val fragment = FuLiPoolFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId() = R.layout.fu_li_pool_fragment

    private var todayIncome = ""

    private var incomed = ""

    private var inComeTv: TextView? = null

    private var sst = AppGlobals.getApplication().getString(R.string.wallet_sst)

    private lateinit var mWaterView: FloatBallView

    private val viewsClickIds = arrayOf(
        R.id.gonglv,
        R.id.shouyi,
        R.id.tongzhi,
        R.id.xurujilv,
        R.id.dangqianfuli,
        R.id.llishifuli,
    )

    private var waters = mutableListOf<FloatBall>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        inComeTv = view.findViewById(R.id.inComeTv)
        view.findViewById<View>(R.id.mToolbar)
            .setOnClickListener { onBackPressedSupport() }

        mWaterView = view.findViewById(R.id.mWaterView)

//        val str0 = "今日新增"
//        val str1 = "\$200.00"
//        waters.add(
//            FloatBall(
//                JssSpannableString(_mActivity, "$str0\n$str1")
//                    .first(str0)
//                    .textColor(R.color.nav_bar_bg_color)
//                    .size(9)
//                    .first(str1)
//                    .textColor(R.color.nav_bar_bg_color)
//                    .size(11)
//                    .bold()
//            )
//        )
//        mWaterView.setDatas(waters)
//
        mWaterView.setItemClickedDismiss(false)

        viewsClickIds.forEach {
            view.findViewById<View>(it)
                .setOnClickListener(this)
        }
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val deviceTokens = Preference.HOLDER.getCustomAppProfile(CURRENT_DEVICE_TOKEN)
        val map = mapOf(
            "deviceToken" to deviceTokens,
            "uid" to userInfo?.uid
        )

        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.welfareIndex)
            .success {
                parseWelfareIndex(it)
            }
            .failure {
                normalShortToast(_mActivity,R.string.get_fu_li_info_failed)
//                Toast.makeText(_mActivity, "获取福利池失败", Toast.LENGTH_LONG).show()
                onBackPressedSupport()
            }
            .error { _, msg ->
                normalShortToast(_mActivity,msg)
//                Toast.makeText(_mActivity, msg, Toast.LENGTH_LONG).show()
                onBackPressedSupport()
            }.build().post()

    }

    private fun parseWelfareIndex(it: String?) {
        val obj = JSON.parseObject(it)
        todayIncome = obj["todayIncome"].toString()
        incomed = obj["incomed"].toString()
        inComeTv?.text = incomed
        showBall()

    }

    private fun showBall() {
//        if (todayIncome.isDigitsOnly()) {
//            val todayIncomeValue = todayIncome.toDouble()
//            if (todayIncomeValue > 0.0) {
//
//            }
//        }

        val str0 = "今日新增"
        val str1 = "$todayIncome $sst"
        waters.add(
            FloatBall(
                JssSpannableString(_mActivity, "$str0\n$str1")
                    .first(str0)
                    .textColor(R.color.yellow_color_2)
                    .size(9)
                    .first(str1)
                    .textColor(R.color.yellow_color_2)
                    .size(11)
                    .bold()
            )
        )
        mWaterView.setDatas(waters)

    }

    override fun onClick(p0: View?) {

        when (p0?.id) {
            R.id.gonglv -> {
                //攻略
                start(FuLiPoolHelperFragment.newInstance())
            }
            R.id.shouyi -> {
                //收益
                start(ShouYiFragment.newInstance())
            }
            R.id.tongzhi -> {
                //通知
                start(TongZhiFragment.newInstance())
            }
            R.id.xurujilv -> {
                //蓄入记录
                start(XuRuJiLvFragment.newInstance())
            }
            R.id.dangqianfuli -> {
                //当前福利
                start(DangQianFuLiFragment.newInstance())
            }
            R.id.llishifuli -> {
                //历史福利
                start(LiShiFuLiFragment.newInstance())
            }
        }
    }
}